// const BASE_URL = 'http://192.168.0.13:6980/';
const BASE_URL = 'http://120.236.168.162:2021/';

function request({ url, method = 'GET', data = {}, header = {} }) {

  const isAbsoluteURL = url.startsWith('http://') || url.startsWith('https://')
  const fullUrl = isAbsoluteURL ? url : BASE_URL + url
  return new Promise((resolve, reject) => {
    uni.request({
      url: fullUrl,
      method,
      data,
      header: {
        'Content-Type': 'application/json',
        ...header
      },
      success(res) {
        if (res.statusCode >= 200 && res.statusCode < 300) {
          resolve(res.data);
        } else {
          uni.showToast({ title: '请求失败', icon: 'none' });
          reject(new Error(`请求异常：${res.errMsg}`));
        }
      },
      fail(error) {
        uni.showToast({ title: '网络异常', icon: 'none' });
        reject(error);
      }
    });
  });
}

export default request;